(a, b) = map(int, input().split(' '))
(c, d) = map(int, input().split(' '))
first_b = b
first_d = d
pick_d = 0
pick_b = 0
not_pick_b = 0
not_pick_d = 0
if b == d:
print(b)
else:
while True:
if (b - first_d) % c == 0 and b - first_d != 0 and (b - first_d) // c > 0:
pick_b += 1
break
else:
if b > 10000:
not_pick_b += 1
break
b += a
while True:
if (d - first_b) % a == 0 and (d - first_b)!= 0 and (d - first_b) // a > 0:
pick_d += 1
break
else:
if d > 10000:
not_pick_d += 1
break
d += c
if pick_b == pick_d == 1:
print(min(b, d))
if not_pick_b == not_pick_d == 1:
print('-1')
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define debug(a) cout << #a << " = " << a << endl
#define debugsl(a) cout << #a << " = " << a << ", "
#define rep(i,a,b) for(int i = (a); i <= (b); i++)
#define repa(i,a,b) for(int i = (a); i >= (b); i--)
#define pll pair<lli,lli>
#define pb push_back
#define MAX 100
lli a,b,c,d,dif;
map<lli,lli> mapa;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> a >> b >> c >> d;
dif = d-b;
while (true) {
if (mapa[dif] == 1) {
b = -1;
break;
}
mapa[dif] = 1;
if (dif == 0) break;
if (dif < 0) {
dif += c;
d += c;
}
else if (dif > 0) {
dif -= a;
b += a;
}
}
cout << b;
return 0;
}
574A - Bear and Elections | 352B - Jeff and Periods |
1244A - Pens and Pencils | 1670A - Prof Slim |
1189A - Keanu Reeves | 678A - Johny Likes Numbers |
1699C - The Third Problem | 1697D - Guess The String |
754B - Ilya and tic-tac-toe game | 760A - Petr and a calendar |
1573A - Countdown | 166A - Rank List |
1631B - Fun with Even Subarrays | 727A - Transformation from A to B |
822B - Crossword solving | 1623A - Robot Cleaner |
884B - Japanese Crosswords Strike Back | 862B - Mahmoud and Ehab and the bipartiteness |
429A - Xor-tree | 1675C - Detective Task |
950A - Left-handers Right-handers and Ambidexters | 672B - Different is Good |
1C - Ancient Berland Circus | 721A - One-dimensional Japanese Crossword |
1715B - Beautiful Array | 60B - Serial Time |
453A - Little Pony and Expected Maximum | 1715A - Crossmarket |
1715C - Monoblock | 1512C - A-B Palindrome |